/****************************** (C) COPYRIGHT 2016 Rsestudio ****************************
* File Name          : 
* Author             : RSES_Liyang
* Version            : V1.0.0
* Date               : 00-May-20
* Description        : 
*****************************************************************************************/
/* Includes ----------------------------------------------------------------------------*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "Bsp.h"
#include "PowerManager.h"
#include "main.h"
#include "W25X.h"
#include "MEAS.h"
#include "button.h"
#include "PowerManager.h"
#include "device_pins.h"
#include "UI.h"
#include "UI_MainPage.h"
#include "oled.h"
#include "oled_font.h"

#include "AS5311.h"
/* Extern variables --------------------------------------------------------------------*/
/* Private define ----------------------------------------------------------------------*/
/* Private macro -----------------------------------------------------------------------*/
/* Private typedef ---------------------------------------------------------------------*/
/* Private variables -------------------------------------------------------------------*/
static TimerHandle_t ui_timer = NULL;
static StaticTimer_t xui_timer;
static EVENT_SRCEEN event_screen;
static void cb_ui_timer(TimerHandle_t parameter);
static SemaphoreHandle_t mtx_process_event = NULL;     // 互斥量句柄
static StaticSemaphore_t xmtx_process_event;  // 存储互斥量的数据结构


//static uint16_t page_change_timer;
/* Private functions -------------------------------------------------------------------*/
static HMI_ENGINE_RESULT	UI_Initialize(SGUI_SCR_DEV *pDev);
static HMI_ENGINE_RESULT	UI_ExitScreen(SGUI_SCR_DEV *pDev);
static HMI_ENGINE_RESULT	UI_Prepare(SGUI_SCR_DEV *pDev,const void* pParameters);
static HMI_ENGINE_RESULT	UI_Refresh(SGUI_SCR_DEV *pDev,const void* pParameters);
static HMI_ENGINE_RESULT	UI_ProcessEvent(SGUI_SCR_DEV *pDev,const HMI_EVENT_BASE* pEvent,SGUI_INT* pActionID);
static HMI_ENGINE_RESULT	UI_PostProcess(SGUI_SCR_DEV *pDev,HMI_ENGINE_RESULT eProcResult,SGUI_INT ActionID);

static uint16_t StabyTimer;

static uint8_t rdy_start;
extern const uint8_t start_cancel[];
extern const uint8_t ble_conn[];


static HMI_SCREEN_ACTION	UI_action = 
{
	UI_Initialize,
	UI_Prepare,
	UI_Refresh,
	UI_ProcessEvent,
	UI_PostProcess,
	UI_ExitScreen,
};

HMI_SCREEN_OBJECT		UI_MainPage = 
{
	UI_SCREEN_ID_Main_Parge,
	&UI_action
};

/*****************************************************************************************
* Function Name  : UI_Welcome_Initialize
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Initialize(SGUI_SCR_DEV *pDev)
{	
	
	mtx_process_event = xSemaphoreCreateMutexStatic(&xmtx_process_event);
	
    ui_timer = xTimerCreateStatic("tm_ui_period", pdMS_TO_TICKS(30), pdTRUE, NULL, cb_ui_timer, &xui_timer);
	
	HMI_EVENT_INIT(event_screen);
	
	return HMI_RET_NORMAL;
}
//

/*****************************************************************************************
* Function Name  : UI_ExitScreen
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_ExitScreen(SGUI_SCR_DEV *pDev)
{	
	xTimerStop(ui_timer, portMAX_DELAY);
	
#if DEV_TYPE == DEV_TYPE_I7
//	OLED_Clear();
//	OLED_Refresh();
#elif DEV_TYPE == DEV_TYPE_XP
//	rses_led_control(&led_sys_r,0);
//	rses_led_control(&led_sys_g,0);
#elif DEV_TYPE == DEV_TYPE_MINI
//	rses_led_control(&led_sys_r,0);
//	rses_led_control(&led_sys_g,0);
#elif DEV_TYPE == DEV_TYPE_VACUUM

#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD
    
#elif DEV_TYPE == DEV_TYPE_RPT_STD	
#else
#error
#endif
	vTaskDelay(pdMS_TO_TICKS(100));    /* 延时ms */
	return HMI_RET_NORMAL;
}

void ui_disp_group(uint8_t group)
{
	lcd_fillarea(0,21,127,42,0);
	if(group>=9)
	{
		OLED_ShowNum(40,32,1+group,2,24,0);
	}
	else
	{		
		OLED_ShowNum(40+15,32,1+group,1,24,0);
	}              						
}



void ui_disp_dist_frame(void)
{
    uint8_t i;
    uint8_t x,y;
    
    x = 0;
    y = 42;
    for(i=0;i<62;i++)
    {
        OLED_DrawPoint(x + i,y,1);
        OLED_DrawPoint(x + i,y+1,1);
    }
    
    for(i=0;i<41;i++)
    {
       OLED_DrawPoint(63, 23 + i,1); 
       OLED_DrawPoint(64, 23 + i,1); 
    }
    
}

void ui_disp_dist_up(uint8_t x,uint8_t y,int32_t dist)
{
    int16_t bit_buf[6];

	lcd_fillarea(x,y,40,11,0);
    
    bit_buf[0] = dist/100%10;
    bit_buf[1] = dist/10%10;
    bit_buf[2] = dist/1%10;
    
    
    lcd_fillBitmap(x+(0*13),y,11,18,(uint8_t *)&Font_Num11x18[bit_buf[0]],0);
    lcd_fillBitmap(x+(1*13),y,11,18,(uint8_t *)&Font_Num11x18[bit_buf[1]],0);
    lcd_fillBitmap(x+(2*13),y,11,18,(uint8_t *)&Font_Num11x18[bit_buf[2]],0);
              						
}

void ui_disp_dist_down(uint8_t x,uint8_t y,int32_t dist)
{
    int16_t bit_buf[6];
    int32_t temp_16;
    uint8_t minus_flag;
    
	lcd_fillarea(x,y,40,11,0);
    
    minus_flag = 0;
    
    dist = dist/10;
    
    temp_16 = dist;
    if(dist < 0)
    {
        minus_flag = 1;
        temp_16 = dist * -1;
    }

    
    bit_buf[0] = temp_16/1000%10;
    bit_buf[1] = temp_16/100%10;
    bit_buf[2] = temp_16/10%10;
    bit_buf[3] = temp_16/1%10;
    
    if(minus_flag == 0)
    {
        lcd_fillBitmap(x+(0),y + 6,6,6,(uint8_t *)&Font_Num11x18_plus_6x6,0);
    }
    else
    {
        lcd_fillBitmap(x+(0),y + 6,6,6,(uint8_t *)&Font_Num11x18_minus_6x6,0);
    }
    
      
    lcd_fillBitmap(x+(0*13 + 7),y,11,18,(uint8_t *)&Font_Num11x18[bit_buf[0]],0);
    lcd_fillBitmap(x+(1*13 + 7),y,11,18,(uint8_t *)&Font_Num11x18[bit_buf[1]],0);
    lcd_fillBitmap(x+(2*13 + 7),y,11,18,(uint8_t *)&Font_Num11x18[bit_buf[2]],0);
    lcd_fillBitmap(x+(3*13 + 6),y + 14,4,4,(uint8_t *)&Font_Num11x18_point_4x4,0);
    
    lcd_fillBitmap(x+(3*13 + 11),y,11,18,(uint8_t *)&Font_Num11x18[bit_buf[3]],0);
}


void ui_disp_angle_pic_y(uint8_t x,uint8_t y,int16_t angle)
{
    uint8_t i;
    int16_t temp_16;
    uint8_t point_pos;
    uint8_t point_flag;
    lcd_fillarea(x,y,8,40,0);
    
    angle = angle/10;
    point_flag = 1;
    if((angle>-7)&&(angle<7))
    {
        point_flag = 0;
        angle = 0;
    }
    
    temp_16 = angle + 103;
    
    if(temp_16<0)
    {
        temp_16 = 0;
    }
    else if(temp_16 > 200)
    {
        temp_16 = 200;
    }
    
    
    
    point_pos = temp_16/6.25;
    
    
    OLED_DrawPoint(x+2,y+0,1);
    OLED_DrawPoint(x+3,y+0,1);
    OLED_DrawPoint(x+4,y+0,1);
    OLED_DrawPoint(x+5,y+0,1);
    
    OLED_DrawPoint(x+1,y+1,1);
    OLED_DrawPoint(x+2,y+1,1);
    OLED_DrawPoint(x+3,y+1,1);
    OLED_DrawPoint(x+4,y+1,1);   
    OLED_DrawPoint(x+5,y+1,1);   
    OLED_DrawPoint(x+6,y+1,1);   
    //---
    OLED_DrawPoint(x+1,y + 36 + 2,1);
    OLED_DrawPoint(x+2,y + 36 + 2,1);
    OLED_DrawPoint(x+3,y + 36 + 2,1);
    OLED_DrawPoint(x+4,y + 36 + 2,1);
    OLED_DrawPoint(x+5,y + 36 + 2,1);
    OLED_DrawPoint(x+6,y + 36 + 2,1);
    
    OLED_DrawPoint(x+2,y + 36 + 3,1);
    OLED_DrawPoint(x+3,y + 36 + 3,1);
    OLED_DrawPoint(x+4,y + 36 + 3,1);
    OLED_DrawPoint(x+5,y + 36 + 3,1);
    
    
    //----------------------------------
    OLED_DrawPoint(x+3,y + 2 + point_pos,1);
    OLED_DrawPoint(x+4,y + 2 + point_pos,1);
    
    OLED_DrawPoint(x+2,y + 2 + point_pos + 1,1);    
    OLED_DrawPoint(x+3,y + 2 + point_pos + 1,point_flag);
    OLED_DrawPoint(x+4,y + 2 + point_pos + 1,point_flag); 
    OLED_DrawPoint(x+5,y + 2 + point_pos + 1,1);
    
    OLED_DrawPoint(x+2,y + 2 + point_pos + 2,1);
    OLED_DrawPoint(x+3,y + 2 + point_pos + 2,point_flag);
    OLED_DrawPoint(x+4,y + 2 + point_pos + 2,point_flag);
    OLED_DrawPoint(x+5,y + 2 + point_pos + 2,1);
    
    OLED_DrawPoint(x+3,y + 2 + point_pos + 3,1);
    OLED_DrawPoint(x+4,y + 2 + point_pos + 3,1);
    
      
    for(i=0;i<36;i++)
    {
        OLED_DrawPoint(x+0,y + i + 2,1);
        OLED_DrawPoint(x+1,y + i + 2,1);
        
        OLED_DrawPoint(x+6,y + i + 2,1);
        OLED_DrawPoint(x+7,y + i + 2,1);
    }
    
    OLED_DrawPoint(x+1,y + 17 + 2,0);
    OLED_DrawPoint(x+1,y + 18 + 2,0);
    
    OLED_DrawPoint(x+6,y + 17 + 2,0);
    OLED_DrawPoint(x+6,y + 18 + 2,0);
}

void ui_disp_angle_pic_x(uint8_t x,uint8_t y,int16_t angle)
{
    uint8_t i;
    int16_t temp_16;
    uint8_t point_pos;
    uint8_t point_flag;
    lcd_fillarea(x,y,40,8,0);
    
    angle = angle/10;
    point_flag = 1;
    if((angle>-7)&&(angle<7))
    {
        point_flag = 0;
        angle = 0;
    }
    
    temp_16 = angle + 103;
    
    if(temp_16<0)
    {
        temp_16 = 0;
    }
    else if(temp_16 > 200)
    {
        temp_16 = 200;
    }
    
    
    
    point_pos = temp_16/6.25;
    
    
    OLED_DrawPoint(x+0,y+2,1);
    OLED_DrawPoint(x+0,y+3,1);
    OLED_DrawPoint(x+0,y+4,1);
    OLED_DrawPoint(x+0,y+5,1);
    
    OLED_DrawPoint(x+1,y+1,1);
    OLED_DrawPoint(x+1,y+2,1);
    OLED_DrawPoint(x+1,y+3,1);
    OLED_DrawPoint(x+1,y+4,1);   
    OLED_DrawPoint(x+1,y+5,1);   
    OLED_DrawPoint(x+1,y+6,1);   
    //---
    OLED_DrawPoint(x+2 + 36,y+1,1);
    OLED_DrawPoint(x+2 + 36,y+2,1);
    OLED_DrawPoint(x+2 + 36,y+3,1);
    OLED_DrawPoint(x+2 + 36,y+4,1);
    OLED_DrawPoint(x+2 + 36,y+5,1);
    OLED_DrawPoint(x+2 + 36,y+6,1);
    
    OLED_DrawPoint(x+3 + 36,y+2,1);
    OLED_DrawPoint(x+3 + 36,y+3,1);
    OLED_DrawPoint(x+3 + 36,y+4,1);
    OLED_DrawPoint(x+3 + 36,y+5,1);
    
    //----------------------------------
    OLED_DrawPoint(x+ 2 + point_pos,y + 3,1);
    OLED_DrawPoint(x+ 2 + point_pos,y + 4,1);
    
    OLED_DrawPoint(x+ 3 + point_pos,y + 2,1);    
    OLED_DrawPoint(x+ 3 + point_pos,y + 3,point_flag);
    OLED_DrawPoint(x+ 3 + point_pos,y + 4,point_flag); 
    OLED_DrawPoint(x+ 3 + point_pos,y + 5,1);
    
    OLED_DrawPoint(x+ 4 + point_pos,y + 2,1);
    OLED_DrawPoint(x+ 4 + point_pos,y + 3,point_flag);
    OLED_DrawPoint(x+ 4 + point_pos,y + 4,point_flag);
    OLED_DrawPoint(x+ 4 + point_pos,y + 5,1);
    
    OLED_DrawPoint(x+ 5 + point_pos,y + 3,1);
    OLED_DrawPoint(x+ 5 + point_pos,y + 4,1);
    
      
    for(i=0;i<36;i++)
    {
        OLED_DrawPoint(x+2 + i,y + 0,1);
        OLED_DrawPoint(x+2 + i,y + 1,1);
        
        OLED_DrawPoint(x+2 + i,y + 6,1);
        OLED_DrawPoint(x+2 + i,y + 7,1);
    }
    
    OLED_DrawPoint(x+2 + 17,y + 1,0);
    OLED_DrawPoint(x+2 + 18,y + 1,0);
    
    OLED_DrawPoint(x+2 + 17,y + 6,0);
    OLED_DrawPoint(x+2 + 18,y + 6,0);
}

uint8_t test_x,test_y;
uint8_t test_x1,test_y1;
/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Prepare(SGUI_SCR_DEV *pDev,const void* pParameters)
{
	xTimerStart(ui_timer, portMAX_DELAY);	
	
	rdy_start = 0;
    
	(void)rdy_start;
    
    test_x = 11;
    test_y = 22;
    
    test_x1 = 0;
    test_y1 = 46;
    
    iProfile.temperature.new_inster_flag = 0;
    
#if DEV_TYPE == DEV_TYPE_I7
	OLED_Clear();
	refresh_ui_head(1);
	ui_disp_group(iProfile.sys_status.Group);
	OLED_Refresh();       		
#elif DEV_TYPE == DEV_TYPE_XP
//    rses_led_control(&led_sys,LED_COLOR_BLACK);
//    rses_led_control(&led_ble,LED_COLOR_BLACK);
    rses_led_control(&led_run,LED_COLOR_BLACK); 
#elif DEV_TYPE == DEV_TYPE_MINI
//	rses_led_control(&led_sys_r,0);
//	rses_led_control(&led_sys_g,0);
    rses_led_control(&led_run,LED_COLOR_BLACK); 
#elif DEV_TYPE == DEV_TYPE_VACUUM
	//rses_led_control(&led_sys,LED_COLOR_BLACK);
	//rses_led_control(&led_ble,LED_COLOR_BLACK);
	rses_led_control(&led_run,LED_COLOR_BLACK);
#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD
	rses_led_control(&led_sys,LED_COLOR_GREEN);
	rses_led_control(&led_ble,LED_COLOR_BLACK);
	rses_led_control(&led_run,LED_COLOR_BLACK);
    
#elif DEV_TYPE == DEV_TYPE_RPT_STD
	OLED_Clear();
	refresh_ui_head(1);
    
	OLED_Refresh();  
#else
#error
#endif
	
	iProfile.cmd_ctrl.Meas_ctrl = COMMAND_CTRL_NONE;
	iProfile.cmd_ctrl.Status = COMMAND_CTRL_STS_IDLE;
	iProfile.cmd_ctrl.Recorder_cnt = 0;
	StabyTimer  = 0;
	//new_TcInsert_Ch = 0;
	
	return HMI_RET_NORMAL;;
}
//
extern int32_t vib_pp_data[];

int16_t test_1;
int16_t test_2;
int16_t test_3;
int16_t test_4;
uint32_t sqrt_in;
uint32_t sqrt_out;

int16_t atan2_out;
int16_t atan2_in_x;
int16_t atan2_in_y;
/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Refresh(SGUI_SCR_DEV *pDev,const void* pParameters)
{	
	static uint16_t low_power_off_timer;	
    
//    sqrt_in = rs_sqrt(sqrt_out);
//    
//    atan2_out = atan2s16(atan2_in_x,atan2_in_y);
#if DEV_TYPE == DEV_TYPE_I7
	
	if(iProfile.temperature.new_inster_flag != 0)
	{
        iProfile.temperature.new_inster_flag = 0;
		HMI_SwitchScreen(UI_SCREEN_ID_TCINSERT,NULL);
		return HMI_RET_NORMAL;
	}
	
	refresh_ui_head(0);
    
    if(rdy_start != 0)
    {
        lcd_fillarea(0,21,128,42,0);//clean
        lcd_fillBitmap(20,29,19,10,(uint8_t*)up_char,0);
        lcd_fillBitmap(44,27,46,15,(uint8_t*)start_char,0);
        lcd_fillBitmap(20,49,19,10,(uint8_t*)dn_char,0);
        lcd_fillBitmap(44,45 ,67,15,(uint8_t*)cancel_char,0);
    }
    else
    {
        ui_disp_group(iProfile.sys_status.Group);
    } 

//    OLED_ShowNum(85,24,iProfile.vibrate.vib_x,5,12,0);
//    OLED_ShowNum(85,36,iProfile.vibrate.vib_y,5,12,0);
//    OLED_ShowNum(85,48,iProfile.vibrate.vib_z,5,12,0);
    
    
	OLED_Refresh();  
	
	if((iProfile.sys_status.BatChargeStatus == 0)&&(iProfile.sys_status.BatLevelStatus < 5)&&(iProfile.sys_status.BatVoltage < 3400))
	{
		if(low_power_off_timer++>50)
		{
            log_write(EFLOG_LVL_INFO,LOG_TAG_POWER,LOG_POWER_OFF_LOWBAT,0,0,0,0);
			// Power_OFF();
		}	
	}
	else
	{
		low_power_off_timer = 0;
	}

	if(++StabyTimer>((300*1000)/100))
	{
        log_write(EFLOG_LVL_INFO,LOG_TAG_POWER,LOG_POWER_OFF_TIMEOUT,0,0,0,0);
		// Power_OFF();
		//HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);  	
	}
	
	if(iProfile.sys_status.VbusOnline!=0)
	{
		StabyTimer=0;
	}
	
	if(iProfile.Ble.Status >=2)
	{
		StabyTimer=0;
	}
		
#elif DEV_TYPE == DEV_TYPE_XP

    if(WIFI_Connetstatusflag == 2)
    {
        rses_led_control(&led_ble,LED_COLOR_BLUE);
    }
    else
    {
        rses_led_control(&led_ble,LED_COLOR_BLACK);
    }
    
    if(iProfile.sys_status.VbusOnline)
    {
        if(iProfile.sys_status.BatChargeStatus == 1)
        {
            rses_led_control(&led_sys,LED_COLOR_RED);
        }
        else
        {
            rses_led_control(&led_sys,LED_COLOR_GREEN);
        }
    }
    else
    {
        if((iProfile.sys_status.BatLevelStatus < 10)&&(iProfile.sys_status.BatVoltage < 3400))
        {
            rses_led_control(&led_sys,LED_COLOR_RED);
        }
        else
        {
            rses_led_control(&led_sys,LED_COLOR_GREEN);
        }       
    }	
        
    if((iProfile.sys_status.BatChargeStatus == 0)&&(iProfile.sys_status.BatLevelStatus < 5)&&(iProfile.sys_status.BatVoltage < 3400))
	{
		if(low_power_off_timer++>50)
		{
			Power_OFF();
		}	
	}
	else
	{
		low_power_off_timer = 0;
	}

	if(++StabyTimer>((300*1000)/100))
	{
		Power_OFF();
		//HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);  	
	}
	
	if(iProfile.sys_status.VbusOnline!=0)
	{
		StabyTimer=0;
	}
	
	if(iProfile.Ble.Status >=2)
	{
		StabyTimer=0;
	}
	
#elif DEV_TYPE == DEV_TYPE_MINI
	
	if(iProfile.Ble.Status == 2)
    {
        rses_led_control(&led_ble,LED_COLOR_BLUE);
    }
    else
    {
        rses_led_control(&led_ble,LED_COLOR_BLACK);
    }
    
    if(iProfile.sys_status.VbusOnline)
    {
        if(iProfile.sys_status.BatChargeStatus == 1)
        {
            rses_led_control(&led_sys,LED_COLOR_RED);
        }
        else
        {
            rses_led_control(&led_sys,LED_COLOR_GREEN);
        }
        }
    else
    {
        if((iProfile.sys_status.BatLevelStatus < 10)&&(iProfile.sys_status.BatVoltage < 3400))
        {
            rses_led_control(&led_sys,LED_COLOR_RED);
        }
        else
        {
            rses_led_control(&led_sys,LED_COLOR_GREEN);
        }       
    }	
        
    if((iProfile.sys_status.BatChargeStatus == 0)&&(iProfile.sys_status.BatLevelStatus < 5)&&(iProfile.sys_status.BatVoltage < 3400))
	{
		if(low_power_off_timer++>50)
		{
			Power_OFF();
		}	
	}
	else
	{
		low_power_off_timer = 0;
	}

	if(++StabyTimer>((300*1000)/100))
	{
		Power_OFF();
		//HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);  	
	}
	
	if(iProfile.sys_status.VbusOnline!=0)
	{
		StabyTimer=0;
	}
	
//	if(iProfile.Ble.Status >=2)
//	{
//		StabyTimer=0;
//	}
	
#elif DEV_TYPE == DEV_TYPE_VACUUM

//	if(iProfile.Ble.Status == 2)
//    {
//        rses_led_control(&led_ble,LED_COLOR_BLUE);
//    }
//    else
//    {
//        rses_led_control(&led_ble,LED_COLOR_BLACK);
//    }
    
    if(iProfile.sys_status.VbusOnline)
    {
        if(iProfile.sys_status.BatChargeStatus == 1)
        {
            rses_led_control(&led_sys,LED_COLOR_RED);
        }
        else
        {
            rses_led_control(&led_sys,LED_COLOR_GREEN);
        }
        }
    else
    {
        if((iProfile.sys_status.BatLevelStatus < 10)&&(iProfile.sys_status.BatVoltage < 3400))
        {
            rses_led_control(&led_sys,LED_COLOR_RED);
        }
        else
        {
            rses_led_control(&led_sys,LED_COLOR_GREEN);
        }       
    }	
        
    if((iProfile.sys_status.BatChargeStatus == 0)&&(iProfile.sys_status.BatLevelStatus < 5)&&(iProfile.sys_status.BatVoltage < 3400))
	{
		if(low_power_off_timer++>50)
		{
			Power_OFF();
		}	
	}
	else
	{
		low_power_off_timer = 0;
	}

	if(++StabyTimer>((300*1000)/100))
	{
		Power_OFF();
		//HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);  	
	}
	
	if(iProfile.sys_status.VbusOnline!=0)
	{
		StabyTimer=0;
	}
	
//	if(iProfile.Ble.Status >=2)
//	{
//		StabyTimer=0;
//	}
	
	
	
#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD

		
	if((iProfile.sys_status.BatChargeStatus == 0)&&(iProfile.sys_status.BatLevelStatus < 5)&&(iProfile.sys_status.BatVoltage < 3400))
	{
		if(low_power_off_timer++>50)
		{
			Power_OFF();
		}	
	}
	else
	{
		low_power_off_timer = 0;
	}

	if(++StabyTimer>((300*1000)/100))
	{
		Power_OFF();
		//HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);  	
	}
	
	if(iProfile.sys_status.VbusOnline!=0)
	{
		StabyTimer=0;
	}
	
	if(iProfile.Ble.Status >=2)
	{
		StabyTimer=0;
	}

#elif DEV_TYPE == DEV_TYPE_RPT_STD
	
	if(iProfile.temperature.new_inster_flag != 0)
	{
        iProfile.temperature.new_inster_flag = 0;
		HMI_SwitchScreen(UI_SCREEN_ID_TCINSERT,NULL);
		return HMI_RET_NORMAL;
	}
	
	refresh_ui_head(0);
    
    
    ui_disp_dist_up(11,22,iProfile.r_width.set_width);
    ui_disp_dist_down(0,46,iProfile.r_width.relative);
    ui_disp_dist_frame();
    
    ui_disp_angle_pic_y(68,22,iProfile.vibrate.roll);
    ui_disp_angle_pic_x(84,54,iProfile.vibrate.pitch);
    
//    OLED_ShowNum(85,24,iProfile.vibrate.vib_x,4,12,0);
//    OLED_ShowNum(85,36,iProfile.vibrate.vib_y,4,12,0);
//    OLED_ShowNum(85,48,iProfile.vibrate.vib_z,4,12,0);
    
	OLED_Refresh();  
	
	if((iProfile.sys_status.BatChargeStatus == 0)&&(iProfile.sys_status.BatLevelStatus < 5)&&(iProfile.sys_status.BatVoltage < 3400))
	{
		if(low_power_off_timer++>50)
		{
			Power_OFF();
		}	
	}
	else
	{
		low_power_off_timer = 0;
	}

	if(++StabyTimer>((300*1000)/100))
	{
        log_write(EFLOG_LVL_INFO,LOG_TAG_POWER,LOG_POWER_OFF_MANUAL,0,0,0,0);
		Power_OFF();
		//HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);  	
	}
	
	if(iProfile.sys_status.VbusOnline!=0)
	{
		StabyTimer=0;
	}
	
	if(iProfile.Ble.Status >=2)
	{
		StabyTimer=0;
	}
		
#else
#error
#endif	

	if(iProfile.cmd_ctrl.Meas_ctrl == COMMAND_CTRL_START)
	{
		iProfile.cmd_ctrl.Meas_ctrl = COMMAND_CTRL_NONE;
		HMI_SwitchScreen(UI_SCREEN_ID_CondStart,(int *)COMMAND_CTRL_START);
	}
	else if(iProfile.cmd_ctrl.Meas_ctrl == COMMAND_CTRL_COND_START)
	{
		iProfile.cmd_ctrl.Meas_ctrl = COMMAND_CTRL_NONE;
		HMI_SwitchScreen(UI_SCREEN_ID_CondStart,(int *)COMMAND_CTRL_COND_START);
	}
	else if(iProfile.cmd_ctrl.Meas_ctrl == COMMAND_CTRL_DIRECT_START)
	{
		iProfile.cmd_ctrl.Meas_ctrl = COMMAND_CTRL_NONE;
		HMI_SwitchScreen(UI_SCREEN_ID_CondStart,(int *)COMMAND_CTRL_DIRECT_START);
	}
	else
	{
		iProfile.cmd_ctrl.Meas_ctrl = COMMAND_CTRL_NONE;
	}
	return HMI_RET_NORMAL;
}
//

/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/

HMI_ENGINE_RESULT	UI_ProcessEvent(SGUI_SCR_DEV *pDev,const HMI_EVENT_BASE* pEvent,SGUI_INT* pActionID)
{
	HMI_ENGINE_RESULT		eProcessResult;
	SGUI_UINT16				KeyValue;
	SGUI_UINT16				KeyAction;
	KEY_PRESS_EVENT			*pKeyEvent;
	SGUI_INT				ProcessAction;
	
	xSemaphoreTake(mtx_process_event, portMAX_DELAY);
	
	eProcessResult = HMI_RET_NORMAL;
	ProcessAction = HMI_DEMO_PROC_NO_ACT;

	if(EVENT_TYPE_ACTION == pEvent->iType)
	{
		if(EVENT_ID_KEY_PRESS == pEvent->iID)
		{			
			pKeyEvent = (KEY_PRESS_EVENT*)pEvent;
			KeyValue = EVENT_KEY_ID(pKeyEvent->Data.uiKeyValue);
			KeyAction = EVENT_KEY_ACTION(pKeyEvent->Data.uiKeyValue);	
			
			StabyTimer = 0;

			switch(KeyValue)
			{
				case KEY_PW:
				{
					
#if DEV_TYPE == DEV_TYPE_I7
                    
					if(KeyAction == BUTTON_EVENT_PRESS)
					{
                        if(rdy_start == 0)
                        {
                            rdy_start =1;
                        }
                        else
                        {
                            rdy_start =0;
                        }		
					}
                    
                    if(KeyAction == BUTTON_EVENT_LONG)
					{											
						eProcessResult = HMI_RET_CANCEL_KEY;
						//HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);
						if(BUS_LINK_STS() == 0)
						{
                            log_write(EFLOG_LVL_INFO,LOG_TAG_POWER,LOG_POWER_OFF_MANUAL,0,0,0,0);
							// Power_OFF();
						}
						
					}
                    
#elif DEV_TYPE == DEV_TYPE_XP

                    if(KeyAction == BUTTON_EVENT_LONG)
					{											
						eProcessResult = HMI_RET_CANCEL_KEY;
						//HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);
						if(BUS_LINK_STS() == 0)
						{
							Power_OFF();
						}
						
					}
                    
#elif DEV_TYPE == DEV_TYPE_MINI

                    if(KeyAction == BUTTON_EVENT_LONG)
					{											
						eProcessResult = HMI_RET_CANCEL_KEY;
						//HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);
						if(BUS_LINK_STS() == 0)
						{
							Power_OFF();
						}
						
					}
                    
                    
#elif DEV_TYPE == DEV_TYPE_VACUUM

                    if(KeyAction == BUTTON_EVENT_LONG)
					{											
						eProcessResult = HMI_RET_CANCEL_KEY;
						//HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);
						if(BUS_LINK_STS() == 0)
						{
							Power_OFF();
						}
						
					}
                    
                    if(KeyAction  == BUTTON_EVENT_DOUBLE)
                    {
                        eProcessResult = HMI_RET_CANCEL_KEY;
                        HMI_SwitchScreen(UI_SCREEN_ID_CondStart,NULL);
                    }
                    
#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD

                    if(KeyAction == BUTTON_EVENT_LONG)
					{											
						eProcessResult = HMI_RET_CANCEL_KEY;
						//HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);
						if(BUS_LINK_STS() == 0)
						{
                            log_write(EFLOG_LVL_INFO,LOG_TAG_POWER,LOG_POWER_OFF_MANUAL,0,0,0,0);
                            
							Power_OFF();
						}				
					}
                    
#elif DEV_TYPE == DEV_TYPE_RPT_STD

                    if(KeyAction == BUTTON_EVENT_LONG)
					{											
						eProcessResult = HMI_RET_CANCEL_KEY;
						//HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);
						if(BUS_LINK_STS() == 0)
						{
							Power_OFF();
						}					
					}  
                    
                    else
					{
                        if(KeyAction  == BUTTON_EVENT_DOUBLE)
                        {
                            eProcessResult = HMI_RET_CANCEL_KEY;
                            HMI_SwitchScreen(UI_SCREEN_ID_CondStart,NULL);
                        }
                    }      
                    
#else
#error
#endif                   
				}break;

				case KEY_UP:
				{	
                    
#if DEV_TYPE == DEV_TYPE_I7
					if(rdy_start)
					{
						if(KeyAction == BUTTON_EVENT_PRESS)
						{
							eProcessResult = HMI_RET_CANCEL_KEY;
							HMI_SwitchScreen(UI_SCREEN_ID_CondStart,NULL);
						}
					}
					else
					{
                        if(KeyAction  == BUTTON_EVENT_DOUBLE)
                        {
                            eProcessResult = HMI_RET_CANCEL_KEY;
                            //oled_config();
                        }
                            
						if(KeyAction  == BUTTON_EVENT_PRESS || KeyAction == BUTTON_EVENT_LONG_REPEAT)
						{					  
							iProfile.sys_status.Group++;
                            
							if(iProfile.sys_status.Group>=MAX_RECODER_GROUP_CNT)
                            {
								iProfile.sys_status.Group=0;
                            }    
						}
					}
#elif DEV_TYPE == DEV_TYPE_XP

					if(KeyAction == BUTTON_EVENT_LONG)
					{
						eProcessResult = HMI_RET_CANCEL_KEY;
						HMI_SwitchScreen(UI_SCREEN_ID_CondStart,NULL);
					}
                    
#elif DEV_TYPE == DEV_TYPE_MINI
					if(KeyAction == BUTTON_EVENT_LONG)
					{
						eProcessResult = HMI_RET_CANCEL_KEY;					
						HMI_SwitchScreen(UI_SCREEN_ID_CondStart,NULL);
					}	
#elif DEV_TYPE == DEV_TYPE_VACUUM

                    eProcessResult = HMI_RET_CANCEL_KEY;

#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD

					if(KeyAction == BUTTON_EVENT_LONG)
					{
						eProcessResult = HMI_RET_CANCEL_KEY;
                        
                        iProfile.cmd_ctrl.b_meas_select = (MEAS_SELECT_TEMPERATURE|MEAS_SELECT_VIBRATION|MEAS_SELECT_OXYGEN);   //measure oxygen;
                        
						HMI_SwitchScreen(UI_SCREEN_ID_PreStart,NULL);
					}
                    else if(KeyAction == BUTTON_EVENT_DOUBLE)
                    {
                        eProcessResult = HMI_RET_CANCEL_KEY;
                        
                        iProfile.cmd_ctrl.b_meas_select = (MEAS_SELECT_TEMPERATURE|MEAS_SELECT_VIBRATION);   //measure oxygen; 
                        
						HMI_SwitchScreen(UI_SCREEN_ID_CondStart,NULL);
                    }
                    
#elif DEV_TYPE == DEV_TYPE_RPT_STD

                    if(KeyAction  == BUTTON_EVENT_DOUBLE)
                    {
                        //eProcessResult = HMI_RET_CANCEL_KEY;
                        width_set_point(iProfile.r_width.set_width);
                    }
                    else if(KeyAction  == BUTTON_EVENT_ONLY_CLICK)
                    {
                        if(iProfile.r_width.set_width < 700)
                        {
                            iProfile.r_width.set_width += 1;
                        }
                    }
                    else if(KeyAction  == BUTTON_EVENT_LONG_REPEAT)
                    {
                        if(iProfile.r_width.set_width < 700)
                        {
                            iProfile.r_width.set_width += 10;
                        }
                    }
                    
                    if(iProfile.r_width.set_width > 700)
                    {
                        iProfile.r_width.set_width = 700;
                    }                                          
#else
#error
#endif				
				}break;	

				case KEY_DOWN:
				{
    
                    
#if DEV_TYPE == DEV_TYPE_I7
                    
					if(rdy_start)
					{
						if(KeyAction  == BUTTON_EVENT_PRESS)
						{
							rdy_start=0;    
						}
					}
					else
					{
						if(KeyAction  == BUTTON_EVENT_PRESS || KeyAction == BUTTON_EVENT_LONG_REPEAT)
						{
							if(iProfile.sys_status.Group > 0)
								iProfile.sys_status.Group--;
							else 
								iProfile.sys_status.Group=MAX_RECODER_GROUP_CNT-1;
						}
					}
                    
#elif DEV_TYPE == DEV_TYPE_XP

										
                    eProcessResult = HMI_RET_CANCEL_KEY;
                    //HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);
                    
#elif DEV_TYPE == DEV_TYPE_MINI

										
                    eProcessResult = HMI_RET_CANCEL_KEY;
                    //HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);
                    
#elif DEV_TYPE == DEV_TYPE_VACUUM

                    eProcessResult = HMI_RET_CANCEL_KEY;
                    
#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD

									
                    eProcessResult = HMI_RET_CANCEL_KEY;
                    //HMI_SwitchScreen(UI_SCREEN_ID_PowerOff,NULL);

                    
#elif DEV_TYPE == DEV_TYPE_RPT_STD

                    if(KeyAction  == BUTTON_EVENT_DOUBLE)
                    {
                        //eProcessResult = HMI_RET_CANCEL_KEY;
                        width_set_zero();
                    }
                    else if(KeyAction  == BUTTON_EVENT_ONLY_CLICK)
                    {                        
                        if(iProfile.r_width.set_width > 0)
                        {
                            iProfile.r_width.set_width -= 1;
                        }
                    }
                    else if(KeyAction  == BUTTON_EVENT_LONG_REPEAT)
                    {
                        if(iProfile.r_width.set_width > 0)
                        {
                            iProfile.r_width.set_width -= 10;
                        }
                    }  
                    
                    if(iProfile.r_width.set_width < 0)
                    {
                        iProfile.r_width.set_width = 0;
                    }             
#else
#error
#endif      
				}break;
			}	      
		}
		else if(EVENT_ID_SCREEN == pEvent->iID)
		{
			EVENT_SRCEEN *temp_event;
			temp_event = (EVENT_SRCEEN*)pEvent;
			ProcessAction = HMI_DEMO_PROC_NO_ACT;		
			
			if(temp_event->Data == 1)
			{
				UI_Refresh(pDev,NULL);
			}
		}
	}
	else if(EVENT_TYPE_DATA == pEvent->iType)
	{
		
	}

	if(pActionID != NULL)
	{
		*pActionID = ProcessAction;
	}
	
	xSemaphoreGive(mtx_process_event);
	
	return eProcessResult;
}

HMI_ENGINE_RESULT	UI_PostProcess(SGUI_SCR_DEV *pDev,HMI_ENGINE_RESULT eProcResult,SGUI_INT ActionID)
{
	return eProcResult;
}

/*****************************************************************************************
* Function Name  : cb_timer_autoback
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
static void cb_ui_timer(TimerHandle_t parameter)
{
	event_screen.Head.iType =	EVENT_TYPE_ACTION;
	event_screen.Head.iID 	=	EVENT_ID_SCREEN;
	event_screen.Data = 1;	
	HMI_ProcessEvent((HMI_EVENT_BASE*)&event_screen);
}
//

/********************************(C)COPYRIGHT 2016 RSEStudio ****************************/
/*****************************************END OF FILE************************************/

